package com.example.sale.Service.imply;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sale.Mapper.ProductMapper;
import com.example.sale.Service.IProductService;
import com.example.sale.True.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public IPage<Product> searchProduct(Page<Product> page, Map<String, Object> params) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        // 根据传入的params参数，构造查询条件
        if (params.containsKey("name")) {
            String name = (String) params.get("name");
            wrapper.eq("name", name);
        }
        if (params.containsKey("type")) {
            Integer type = (Integer) params.get("type");
            wrapper.eq("type", type);
        }
        if (params.containsKey("price")) {
            String price = (String) params.get("price");
            wrapper.eq("price", price);
        }
        return baseMapper.selectPage(page,wrapper);
    }

    @Override
    public boolean isProductExists(String name) {
        Product byName = productMapper.findByName(name);
        return byName!=null;
    }

    @Override
    public Integer GetFruitNumber(String type) {
        Integer fruitNumber = productMapper.GetFruitNumber(type);
        return  fruitNumber;
    }

    @Override
    public Integer GetVegetablesNumber(String type) {
        Integer vegetablesNumber = productMapper.GetVegetablesNumber(type);
        return vegetablesNumber;
    }

    @Override
    public Integer GetMeatNumber(String type) {
        Integer meatNumber = productMapper.GetMeatNumber(type);
        return meatNumber;
    }

    @Override
    public Integer GetSeafoodNumber(String type) {
        Integer seafoodNumber = productMapper.GetSeafoodNumber(type);
        return seafoodNumber;
    }

    @Override
    public String GetMaxPrice() {
        String MaxNumber = String.valueOf(productMapper.GetMaxPrice());
        return  MaxNumber;
    }

    @Override
    public String GetMinPrice() {
        String MinNumber = String.valueOf(productMapper.GetMinPrice());
        return MinNumber;
    }

    @Override
    public double GetAvgPrice() {
        double AvgNumber = productMapper.GetAvgPrice();
        return AvgNumber;
    }
    @Override
    public double GetTypePrice(String type) {
        double TypePrice = productMapper.GetTypePrice(type);
        return TypePrice;
    }
    @CacheEvict(value = "pageDataProduct", allEntries = true)
    public void clearCache() {
        // 清除所有与 "pageData" 缓存相关的数据
    }
}
